package com.logic.landseaserver.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.service.ISEOService;
import com.logic.landseaserver.ws.dto.SEODTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Author: jack
 * @Date: 2017/5/4
 * @Description: SEO控制类
 */
@Path("/seo")
@Api(value="seo")
public class SEORest {

	@Autowired
	private ISEOService iseoServiceImpl;

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	//增加  http://localhost/system/rest/seo/createSEO
	@POST
	@Path("/createSEO")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Create a new SEO",position = 1)
	public Response createSEO(@ApiParam(value = "param", required = true) SEODTO seoDTO) {
		CommonResultMap map = new CommonResultMap();
        try {
        	int id = iseoServiceImpl.insertSEO(seoDTO);
        	seoDTO.setId(id);
            map.setData(seoDTO);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("createSEO error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("createSEO error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	//查询所有信息
	@POST
	@Path("/list")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list SEO",position = 2)
	public Response list(@ApiParam(value = "param", required = true) QueryUtil queryUtil){
        CommonResultMap map = new CommonResultMap();
        List<SEODTO> seoDTOList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            seoDTOList = (List<SEODTO>)iseoServiceImpl.query(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(seoDTOList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);

        } catch (Exception e) {
            logger.error("list error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();		
	}
	
	//删除
    @DELETE
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "logic delete SEO by set deleted", position = 3)
    public Response logicDelete(@ApiParam(value = "param", required = true) @QueryParam("id") Integer id) {
        CommonResultMap map = new CommonResultMap();
        try {
        	iseoServiceImpl.deleteSEO(id);
            map.setResult(CommonResult.SUCCESS,"delete SEO");
        } catch (Exception e) {
            logger.error("logicDelete error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
	
    //编辑
    @PUT
	@Path("/editSEO")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Edit SEO ")
    public Response updateOrder(@ApiParam(value = "param", required = true) SEODTO seoDTO) {
        CommonResultMap map = new CommonResultMap();
        try {
        	iseoServiceImpl.editSEO(seoDTO);
            map.setResult(CommonResult.SUCCESS,"edit SEO");
        } catch (Exception e) {
            logger.error("updateOrder error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
